﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using Common.Utilities;
using Workflows.Components.Rules.Properties;

namespace Workflows.Components.Rules
{
    /// <summary>
    /// generate a evaluator from in-memory assembly using Code DOM
    /// </summary>
    [Serializable]
    public class CodeRuleCondition : RuleNodeDataBase
    {

        #region members
        private string _AssemblyFilePath;
        /// <summary></summary>
        public string AssemblyFilePath
        {
            get
            {
                return this._AssemblyFilePath;
            }
            set
            {
                this._AssemblyFilePath = value;
            }
        }

        private string _TypeFullName;
        /// <summary></summary>
        public string TypeFullName
        {
            get
            {
                return this._TypeFullName;
            }
            set
            {
                this._TypeFullName = value;
            }
        }

        private string _CodeSnippet;
        /// <summary></summary>
        public string CodeSnippet
        {
            get
            {
                return this._CodeSnippet;
            }
            set
            {
                this._CodeSnippet = value;
            }
        }
        #endregion
        /// <summary></summary>
        public CodeRuleCondition()
            : base()
        {
            this._AssemblyFilePath = string.Empty;
            this._TypeFullName = string.Empty;
            this._CodeSnippet = string.Empty;
            this.Name = "Code Condition";
            this.Description = "Using code to evaluate object";
            this.Img = Resources.Search;
            this.RuleNodeType = Rules.RuleNodeType.Conditional;
        }

        public override System.Xml.XmlNode Serialize(ref System.Xml.XmlDocument xDoc, ref System.Xml.XmlNode parentNode)
        {
            XmlNode xNode = base.Serialize(ref xDoc, ref parentNode);

            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "AssemblyFilePath", this.AssemblyFilePath);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode,"TypeFullName",this.TypeFullName);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "CodeSnippet", this.CodeSnippet);

            return xNode;
        }


        public override RuleNodeDataBase Instantiate(XmlNode xNode)
        {
            CodeRuleCondition codeRuleCondition = new CodeRuleCondition();
            codeRuleCondition.RuleNodeID = new Guid(xNode.Attributes.GetNamedItem("RuleNodeID").Value);
            codeRuleCondition.Name = xNode.Attributes.GetNamedItem("Name").Value;
            codeRuleCondition.Description = xNode.Attributes.GetNamedItem("Description").Value;
            codeRuleCondition.RuleNodeType = (RuleNodeType) int.Parse(xNode.Attributes.GetNamedItem("RuleNodeType").Value);

            codeRuleCondition.AssemblyFilePath = xNode.Attributes.GetNamedItem("AssemblyFilePath").Value;
            codeRuleCondition.TypeFullName = xNode.Attributes.GetNamedItem("TypeFullName").Value;
            codeRuleCondition.CodeSnippet = xNode.Attributes.GetNamedItem("CodeSnippet").Value;

            return codeRuleCondition;
        }

        public override string DescribeInHtml()
        {
            return string.Empty;
        }

        public override string Describe()
        {
            return string.Empty;
        }

    }
}
